import { createStore } from 'vuex'

export default createStore({
  // 定义所需的状态
  state: {
    name: 'jack',
    list: [{
      title: '吃饭',
      complete: false
    }, {
      title: '睡觉',
      complete: false
    }, {
      title: '代码',
      complete: true
    }]
  },
  // 同步修改state，只能定义方法
  mutations: {
    addTodo (state, payload) {
      state.list.push(payload)
    },

    delTodo (state, payload) {
      state.list.splice(payload, 1)
    },

    clearTodo (state, payload) {
      state.list = payload
    },

    setName (state, payload) {
      state.name = payload
      // state.name.slice(0, 1) 如果是数组可以使用切片
    }
  },
  // 异步提交mutation
  actions: {
    asyncSetName (store, params) {
      setTimeout(() => {
        store.commit('setName', params)
      }, 2000)
    }
  },
  // 模块化
  modules: {}
})
